#define _CRT_SECURE_NO_WARNINGS

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;

class Solution {
public:
    int minOperations(vector<int>& nums, int x) {
        sort(nums.begin(), nums.end());
        int n = nums.size();
        int ans = INT_MAX;
        int len = 0;
        int sum = 0;
        int left = 0, right = 0;
        while (right < n || (sum >= x && left < right))
        {
            if (sum > x)
            {
                sum -= nums[left++];
                len--;
            }
            else if (sum < x)
            {
                sum += nums[right++];
                len++;
            }
            else
            {
                if (ans > len)
                    ans = len;
                sum -= nums[left++];
                len--;
            }
        }

        return ans == INT_MAX ? -1 : ans;
    }
};

int main()
{
    vector<int> v{ 6016,5483,541,4325,8149,3515,7865,2209,9623,9763,4052,6540,2123,2074,765,7520,4941,5290,5868,6150,6006,6077,2856,7826,9119 };
    Solution().minOperations(v, 31841);
	return 0;
}